package com.alex.monitor.boot;

import com.alex.monitor.core.alarm.impl.DefaultEmailMonitorAlarm;
import com.alex.monitor.core.alarm.impl.DefaultHttpMonitorAlarm;
import com.alex.monitor.core.alarm.impl.DefaultLogMonitorAlarm;
import com.alex.monitor.core.aspect.AlexMethodMonitorAspect;
import com.alex.monitor.core.properties.AlexMonitorProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 自动装配
 *
 * @author Alex.Liu
 * @date: 2025-02-16 19:43
 * @since 1.0.0
 */
@Configuration
@EnableConfigurationProperties(AlexMonitorProperties.class)
public class AlexMonitorAutoConfiguration {

    /**
     * 参数配置
     */
    private final AlexMonitorProperties properties;

    /**
     * 线程池配置
     */
    private final TaskExecutor executor;

    /**
     * 构造
     *
     * @param properties 参数配置
     * @param executor   线程池配置
     */
    public AlexMonitorAutoConfiguration(AlexMonitorProperties properties, TaskExecutor executor) {
        this.properties = properties;
        this.executor = executor;
    }


    /**
     * 配置AOP
     *
     * @return
     */
    @Bean
    AlexMethodMonitorAspect methodMonitorAspect() {
        return new AlexMethodMonitorAspect(executor, properties);
    }


    /**
     * 默认邮件报警器
     *
     * @return
     */
    @Bean(name = "com.alex.monitor.core.alarm.impl.DefaultEmailMonitorAlarm")
    DefaultEmailMonitorAlarm defaultEmailMonitorAlarm() {
        return new DefaultEmailMonitorAlarm(properties);
    }

    /**
     * 默认http报警器
     *
     * @return
     */
    @Bean(name = "com.alex.monitor.core.alarm.impl.DefaultHttpMonitorAlarm")
    DefaultHttpMonitorAlarm defaultHttpMonitorAlarm() {
        return new DefaultHttpMonitorAlarm(properties);
    }

    /**
     * 默认http报警器
     *
     * @return
     */
    @Bean(name = "com.alex.monitor.core.alarm.impl.DefaultLogMonitorAlarm")
    DefaultLogMonitorAlarm defaultLogMonitorAlarm() {
        return new DefaultLogMonitorAlarm(properties);
    }

    @Bean
    @ConditionalOnMissingBean(TaskExecutor.class)
    TaskExecutor executor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(100);
        executor.setThreadNamePrefix("alex-monitor-");
        return executor;
    }

}